package com.cbgolf.oa.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Point;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.widget.CustomProgressBarDialog;


/**
 * @author Created by lq on 2017/8/21.
 */

public class AnimaUtil {

    private static int loading = 0;
    private static TextView loadingTv;
    private static Handler handler = new Handler();
    private static Runnable r = new Runnable() {
        @Override
        public void run() {
            switch (loading % 4) {
                case 0:
                    loadingTv.setText("加载中");
                    break;
                case 1:
                    loadingTv.setText("加载中 .");
                    break;
                case 2:
                    loadingTv.setText("加载中 . .");
                    break;
                case 3:
                    loadingTv.setText("加载中 . . .");
                    break;
                default:
                    break;
            }
            loading++;
            handler.postDelayed(this, 500);
        }
    };
    public static CustomProgressBarDialog mCpd ;
    public static void showLoading(Activity act, TextView loadingTv) {
        if (null != mCpd && mCpd.isShowing()) {
            return;
        }
        if (null != loadingTv) {
            ViewUtils.setVisible((View) loadingTv.getParent(), true);
            ViewUtils.setVisible(loadingTv, true);
        }
        AnimaUtil.loadingTv = loadingTv;
        handler.removeCallbacks(r);
        mCpd = CustomProgressBarDialog.showProgressDialog(act, "加载中...", true);
        setmCpdCancel();
    }

    public static void showLoading(Activity act) {
        if (null != mCpd && mCpd.isShowing()) {
            return;
        }
        if (null == act) {
            return;
        }
        mCpd = CustomProgressBarDialog.showProgressDialog(act, "加载中...", true);
    }

    public static void showLoading(Activity act, boolean enable) {
        if (null != mCpd && mCpd.isShowing()) {
            return;
        }
        if (null == act) {
            return;
        }
        mCpd = CustomProgressBarDialog.showProgressDialog(act, "加载中...", enable);
    }

    public static void setmCpdCancel() {
        if (null != mCpd) {
            mCpd.setOnCancelListener(dialog -> {
                mCpd = null;
                if (null == loadingTv) {
                    return;
                }
                if (View.VISIBLE == loadingTv.getVisibility()) {
                    loadingTv.setText("加载中...");
                    loading = 0;
                    handler.removeCallbacks(r);
                    handler.postDelayed(r, 0);
                }
            });
        }
    }

    public static void stopLoading() {
        if (null != mCpd) {
            try {
                mCpd.cancel();
                mCpd.dismiss();
                mCpd = null;
            } catch (Exception e) {

            }
        }
        handler.removeCallbacks(r);
        if (null != loadingTv) {
            try {
                loadingTv.setVisibility(View.GONE);
                AnimaUtil.loadingTv = null;
            } catch (Exception e) {

            }
        }
    }

    public static void playAnimation(final Activity context, int[] position, View shopCarIv,
                                     final IAnimWorker worker) {

        final ImageView mImg = new ImageView(context);
        mImg.setImageResource(R.drawable.oval_blue);
        mImg.setScaleType(ImageView.ScaleType.MATRIX);
        ViewGroup.LayoutParams vl = new ViewGroup.LayoutParams(DensityUtil.dip2px(context, 15),
                DensityUtil.dip2px(context, 15));
        mImg.setLayoutParams(vl);
        ViewGroup rootView = (ViewGroup) context.getWindow().getDecorView();
        rootView.addView(mImg);

        int[] des = new int[2];
        shopCarIv.getLocationInWindow(des);

        /*动画开始位置，也就是物品的位置;动画结束的位置，也就是购物车的位置 */
        Point startPosition = new Point(position[0], position[1]);
        Point endPosition = new Point(des[0] + shopCarIv.getWidth() / 2,
                des[1] + shopCarIv.getHeight() / 2);

        int pointX = (startPosition.x + endPosition.x) / 2 - 100;
//        int pointY = startPosition.y - 200;
        int pointY = startPosition.y - 100;
        Point controllPoint = new Point(pointX, pointY);

        /*
         * 属性动画，依靠TypeEvaluator来实现动画效果，其中位移，缩放，渐变，旋转都是可以直接使用
         * 这里是自定义了TypeEvaluator， 我们通过point记录运动的轨迹，然后，物品随着轨迹运动，
         * 一旦轨迹发生变化，就会调用addUpdateListener这个方法，我们不断的获取新的位置，是物品移动
         * */
        ValueAnimator valueAnimator = ValueAnimator
                .ofObject(new BizierEvaluator2(controllPoint), startPosition, endPosition);
        valueAnimator.start();
        valueAnimator.addUpdateListener(valueAnimator1 -> {
            Point point = (Point) valueAnimator1.getAnimatedValue();
            mImg.setX(point.x);
            mImg.setY(point.y);
        });

        /**
         * 动画结束，移除掉小圆圈
         */
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                ViewGroup rootView = (ViewGroup) context.getWindow().getDecorView();
                rootView.removeView(mImg);
                if (null != worker) {
                    worker.onAnimEnd();
                }
            }
        });


    }

    /**
     * 贝塞尔曲线（二阶抛物线）
     * controllPoint 是中间的转折点
     * startValue 是起始的位置
     * endValue 是结束的位置
     */
    public static class BizierEvaluator2 implements TypeEvaluator<Point> {

        private Point controllPoint;

        public BizierEvaluator2(Point controllPoint) {
            this.controllPoint = controllPoint;
        }

        @Override
        public Point evaluate(float t, Point startValue, Point endValue) {
            int x = (int) ((1 - t) * (1 - t) * startValue.x + 2 * t * (1 - t) * controllPoint.x
                    + t * t * endValue.x);
            int y = (int) ((1 - t) * (1 - t) * startValue.y + 2 * t * (1 - t) * controllPoint.y
                    + t * t * endValue.y);
            return new Point(x, y);
        }
    }

    public interface IAnimWorker {

        void onAnimEnd();
    }
}
